package com.example.yogurtsystem.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.yogurtsystem.common.PageResult;
import com.example.yogurtsystem.dto.ResultDTO;
import com.example.yogurtsystem.dto.UserDto;
import com.example.yogurtsystem.entity.Drinks;
import com.example.yogurtsystem.entity.User;
import com.example.yogurtsystem.entity.UserRecord;
import com.example.yogurtsystem.entity.excel.UserData;
import com.example.yogurtsystem.exception.ServiceException;
import com.example.yogurtsystem.listener.UserExcelListener;
import com.example.yogurtsystem.mapper.UserMapper;
import com.example.yogurtsystem.service.UserService;
import com.example.yogurtsystem.utils.BeanHelper;
import com.example.yogurtsystem.utils.PageUtil;
import com.example.yogurtsystem.utils.TableTagBean;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

/**
 * @author ZXB
 * @version 1.0.0
 * @ClassName UserServiceImpl
 * @Description 用户service实现类
 * @Date 2021/8/11 23:38
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    private final Logger log = LoggerFactory.getLogger(getClass());
    @Resource
    private UserMapper userMapper;

    @Override
    public List<UserDto> listUser() {
        return BeanHelper.copyProperties(userMapper.selectList(null), UserDto.class);
    }

    @Override
    public void saveUser(UserDto userDto) {
        if (ObjectUtils.isNotEmpty(userDto)) {
            User user = userMapper.selectOne(new QueryWrapper<User>().lambda().eq(User::getName, userDto.getName().trim()));
            if (ObjectUtils.isNotEmpty(user)) {
                if (log.isInfoEnabled()) {
                    log.debug("------用户名重复------：{}", user.getName());
                }
                throw new ServiceException("用户名重复，换个试试");
            }
            user = BeanHelper.copyProperties(userDto, User.class);
            if (log.isDebugEnabled()) {
                log.debug("------插入用户------：{}", JSON.toJSONString(user));
            }
            userMapper.insert(user);
        }
    }

    @Override
    public void deleteUser(String id) {
        if (ObjectUtils.isNotEmpty(id)) {
            if (log.isDebugEnabled()) {
                log.debug("------删除用户------id：{}", id);
            }
            userMapper.deleteById(id.trim());
        }
    }

    @Override
    public void updateUser(User user) {
        user.setUpdateTime(new Date());
        baseMapper.updateById(user);
    }

    @Override
    public ResultDTO selectUserByLimit(int pageNum, int pageSize, String name) {
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        //如果名称不为空则模糊查找名称
        if (StringUtils.isNotEmpty(name)){
            queryWrapper.like("name",name);
        }
        Page<User> page=new Page<>(pageNum,pageSize);
        IPage<User> iPage = userMapper.selectPage(page, queryWrapper);
        PageResult pageResult = PageUtil.pageResultCopy(iPage);
        return ResultDTO.requestSuccess(pageResult);
    }

//    @Override
//    public PageResult<?> selectUserByLimit(TableTagBean ttb) {
//        int pageNum = ttb.getPage();
//        int pageSize = ttb.getPageSize();
//        String name = ttb.getCondition().get("name");
//        LambdaQueryWrapper<User> queryWrapper = new QueryWrapper<User>().lambda();
//        //如果名称不为空则模糊查找名称
//        if (StringUtils.isNotEmpty(name)) {
//            queryWrapper = queryWrapper.like(User::getName, name);
//        }
//        //降序排序
//        queryWrapper.orderByDesc(User::getUpdateTime);
//        Page<User> page = new Page<>(pageNum, pageSize);
//        IPage<User> iPage = userMapper.selectPage(page, queryWrapper);
//        return PageUtil.pageResultCopy(iPage);
//    }

    @Override
    public void importUserData(MultipartFile file, UserService userService) {
        try {
            InputStream inputStream = file.getInputStream();
            EasyExcel.read(inputStream, UserData.class, new UserExcelListener(userService)).sheet().doRead();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
